﻿@using Vitus.GoogleCalendar.Models
@using System.Text
@using System.Web
@using Newtonsoft.Json

@functions
{
    class GoogleCalendarReference
    {
        [JsonProperty(PropertyName = "googleCalendarId")]
        public string GoogleCalendarId { get; set; }

        [JsonProperty(PropertyName = "className")]
        public string ClassName { get; set; }
    }

    string ToCamelCase(string str)
    {
        return str[0].ToString().ToLower() + str.Substring(1);
    }

    string GetLocalizationFileName()
    {
        var supportedCultures = new[] {
            "ar-ma","ar-sa","ar","bg","ca","cs","da","de-at","de","el","en-au",
            "en-ca","en-gb","es","fa","fi","fr-ca","fr","hi","hr","hu","id","is",
            "it","ja","ko","lt","lv","nl","pl","pt-br","pt","ro","ru","sk","sl",
            "sr-cyrl","sr","sv","th","tr","uk","vi","zh-cn","zh-tw"
        };

        var cultureInfo = System.Globalization.CultureInfo.GetCultureInfo(WorkContext.CurrentCulture);

        var cultureNames = new[] { cultureInfo.Name, cultureInfo.TwoLetterISOLanguageName };

        foreach (var cultureName in cultureNames)
        {
            if (supportedCultures.Contains(cultureName))
            {
                return cultureName;
            }
        }

        return null;
    }
}

@{
    Style.Require("FullCalendar");
    Script.Require("FullCalendar_GoogleCalendar");

    var localizationFileName = GetLocalizationFileName();

    if (localizationFileName != null)
    {
        Script.Include("~/modules/vitus.googlecalendar/scripts/lang/" + localizationFileName + ".js");
    }

    var calendar = Model.Calendar as GoogleCalendarPart;
}

<div id="GoogleCalendar-@calendar.Id"></div>
<p class="pull-right"><a class="btn btn-default" href="@Url.Content("~/kalendar")" title="Kalendář akcí">Přejít na kalendář</a></p>

@if (calendar.GoogleCalendarIds != null)
{
    using (Script.Foot())
    {
        var gcReferences = calendar.GoogleCalendarIds.Split(',')
            .Select(gcUrl => new GoogleCalendarReference
            {
                GoogleCalendarId = gcUrl
            })
            .ToArray();

        if (calendar.GoogleCalendarClasses != null)
        {
            var gcClasses = calendar.GoogleCalendarClasses.Split(',');

            if (gcClasses.Length <= gcReferences.Length)
            {
                for (int x = 0; x < gcClasses.Length; x++)
                {
                    gcReferences[x].ClassName = gcClasses[x];
                }
            }
        }

        <script type="text/javascript">
            //<![CDATA[
            $(document).ready(function () {
                $('#GoogleCalendar-@calendar.Id').fullCalendar({
                    googleCalendarApiKey: '@calendar.GoogleCalendarApiKey',
                    eventSources: @Html.Raw(JsonConvert.SerializeObject(gcReferences)),
                    theme: @calendar.Theme.ToString().ToLower(),
                    defaultView: '@ToCamelCase(calendar.DefaultView.ToString("G"))',
                    header: {
                        left: '@calendar.HeaderLeft',
                        center: '@calendar.HeaderCenter',
                        right: '@calendar.HeaderRight'
                    },
                    weekends: @calendar.Weekends.ToString().ToLower(),
                    weekNumbers: @calendar.WeekNumbers.ToString().ToLower(),
                    allDaySlot: @calendar.AllDaySlot.ToString().ToLower(),
                    minTime: @calendar.MinTime,
                    maxTime: @calendar.MaxTime });
            });
            //]]>
        </script>
    }
}